package com.sunyur.tailor.channel.service.inner.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.order.model.*;
import com.sunyur.tailor.channel.service.inner.order.param.*;
import com.sunyur.tailor.channel.service.inner.order.service.SunyurOrderService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName SunyurOrderServiceImpl
 * @Description 订单接口
 * @Author hao.tian
 * @Date 2021/5/11
 * @Version v1.0
 */
@Service("sunyurOrderService")
public class SunyurOrderServiceImpl implements SunyurOrderService {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurOrderServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurOrderServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 获取订单头 链接
     */
    @Value("${sunyur.api.getOrderHeadInfo}")
    private String getOrderHeadInfo;
    /**
     * 获取订单行 链接
     */
    @Value("${sunyur.api.getOrderListItem}")
    private String getOrderListItem;

    /**
     * 批量获取订单头 链接
     */
    @Value("${sunyur.api.getOrderListOrderByCodes}")
    private String getOrderListOrderByCodes;

    /**
     * 批量获取订单行 链接
     */
    @Value("${sunyur.api.getOrderListOrderItemByCodes}")
    private String getOrderListOrderItemByCodes;

    /**
     * 根据来源单号批量获取订单行 链接
     */
    @Value("${sunyur.api.getListOrderItemBySourceCodes}")
    private String getListOrderItemBySourceCodes;

    /**
     * 根据来源行号批量获取订单行 链接
     */
    @Value("${sunyur.api.getlistOrderItemBySourceItemCodes}")
    private String getlistOrderItemBySourceItemCodes;

    /**
     * 批量修改订单 链接
     */
    @Value("${sunyur.api.updateOrderList}")
    private String updateOrderList;

    /**
     * 批量修改订单结算主体 链接
     */
    @Value("${sunyur.api.updateOrders}")
    private String updateOrders;

    /**
     * 订单取消 链接
     */
    @Value("${sunyur.api.cancelOrder}")
    private String cancelOrder;

    /**
     * 创建订单 链接
     */
    @Value("${sunyur.api.createOrder}")
    private String createOrder;

    /**
     * 订单取消 链接
     */
    @Value("${sunyur.api.removeSourceByOrderItemCodes}")
    private String removeSourceByOrderItemCodes;

    /**
     * 根据订单行编号删除订单行来源信息 链接
     */
    @Value("${sunyur.api.deleteOrderByOrderCodes}")
    private String deleteOrderByOrderCodes;

    @Value("${sunyur.api.getListOrderItemCountByCodes}")
    private String getListOrderItemCountByCodes;

    /**
     * 101-订单-修改订单行【已上线】 链接
     */
    @Value("${sunyur.api.updateOrderItems}")
    private String updateOrderItems;


    /**
     * 获取订单行相关数量[0217上线] 链接
     */
    @Value("${sunyur.api.getOrderItemCount}")
    private String getOrderItemCount;


    /**
     * @Description 获取订单头信息接口
     * @Param getOrderHeadParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.GetOrderHeadModel>
     * @Author hao.tian
     * @Date 2021/5/11
     * @Version v1.0
     */
    @Override
    public ChannelResult<GetOrderHeadModel> getOrderHeadInfo(GetOrderHeadParam getOrderHeadParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl getOrderHeadInfo param is {}", JSON.toJSONString(getOrderHeadParam));
        if (ObjectUtils.isEmpty(getOrderHeadParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl getOrderHeadInfo param is null");
        }
        // 调用主线接口
        SunyurResult<GetOrderHeadModel> sunyurResult = sunyurClient.execPost(getOrderHeadInfo, JSON.toJSONString(getOrderHeadParam), new TypeReference<SunyurResult<GetOrderHeadModel>>() {
        });
        // 主线报错
        if (!"success".equals(sunyurResult.getCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * @Description 获取订单行信息接口
     * @Param getOrderListItemParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.GetOrderListItemModel>
     * @Author hao.tian
     * @Date 2021/5/11
     * @Version v1.0
     */
    @Override
    public ChannelResult<List<GetOrderListItemModel>> getOrderListItem(GetOrderListItemParam getOrderListItemParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl getOrderListItem param is {}", JSON.toJSONString(getOrderListItemParam));
        if (ObjectUtils.isEmpty(getOrderListItemParam) || StringUtils.isBlank(getOrderListItemParam.getOrderCode())) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurOrderServiceImpl getOrderListItem param is null");
        }
        // 设置为每页最大条数
        getOrderListItemParam.setPageSize(100);
        // 调用主线查询采购单头信息接口
        TypeReference<SunyurListResult<GetOrderListItemModel>> typeReference = new TypeReference<SunyurListResult<GetOrderListItemModel>>() {
        };
        SunyurListResult<GetOrderListItemModel> sunyurResult = sunyurClient.execListPost(getOrderListItem, JSON.toJSONString(getOrderListItemParam), typeReference);
        // 主线返回错误
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        // 判断是否存在下一页
        if (!SunyurResultUtil.isNextPage(sunyurResult)) {
            return ChannelListResultUtil.success(sunyurResult.getContent());
        }

        // 返回结果集
        List<GetOrderListItemModel> orderItemModelList = Lists.newArrayList();
        // 将第一次查询的结果收集
        orderItemModelList.addAll(sunyurResult.getContent());
        // 循环查询剩余数据
        for (int i = getOrderListItemParam.getPage() + 1; i <= sunyurResult.getTotalPage(); i++) {
            getOrderListItemParam.setPage(i);
            // 调用主线查询采购单头信息接口
            SunyurListResult<GetOrderListItemModel> sunyurPageResult = sunyurClient.execListPost(getOrderListItem, JSON.toJSONString(getOrderListItemParam), typeReference);
            // 某次调用主线返回错误（小概率，可能网络等原因，此时结果集数据不完整，本次查询失败）
            if (!SunyurResultUtil.isSuccess(sunyurPageResult)) {
                LOGGER_ERROR.error("ggetOrderListItem  loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getOrderListItemParam), JSON.toJSONString(sunyurPageResult));
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurPageResult.getErrorMessage());
            }
            orderItemModelList.addAll(sunyurPageResult.getContent());
        }
        return ChannelListResultUtil.success(orderItemModelList);
    }




    /**
     * @Description 批量获取订单头信息接口
     * @Param getListOrderByCodesParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.GetListOrderByCodesModel>
     * @Author hao.tian
     * @Date 2021/5/11
     * @Version v1.0
     */
    @Override
    public ChannelResult<List<GetListOrderByCodesModel>> geListOrderByCodes(GetListOrderByCodesParam getListOrderByCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl geListOrderByCodes param is {}", JSON.toJSONString(getListOrderByCodesParam));
        if (ObjectUtils.isEmpty(getListOrderByCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl geListOrderByCodes param is null");
        }
        List<GetListOrderByCodesModel> resultList = Lists.newArrayList();
        // 参数按长度分割
        List<List<String>> orderCodes = Lists.partition(getListOrderByCodesParam.getOrderCodes(), 100);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        GetListOrderByCodesParam getListOrderByCodesParamClilent = new GetListOrderByCodesParam();
        TypeReference<SunyurResult<List<GetListOrderByCodesModel>>> typeReference = new TypeReference<SunyurResult<List<GetListOrderByCodesModel>>>() {
        };
        for (List<String> orderCode : orderCodes) {
            getListOrderByCodesParamClilent.setOrderCodes(orderCode);
            // 调用主线接口
            SunyurResult<List<GetListOrderByCodesModel>> sunyurResult = sunyurClient.execPost(getOrderListOrderByCodes, JSON.toJSONString(getListOrderByCodesParamClilent), typeReference);
            if (SunyurResultUtil.isSuccess(sunyurResult)) {
                resultList.addAll(sunyurResult.getContent());
            } else {
                LOGGER_ERROR.error("geListOrderByCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getListOrderByCodesParamClilent), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(resultList);
    }

    /**
     * @Description 批量获取订单行信息接口
     * @Param getListOrderItemByCodesParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.GetListOrderItemByCodesModel>
     * @Author hao.tian
     * @Date 2021/5/11
     * @Version v1.0
     */
    @Override
    public ChannelListResult<GetListOrderItemByCodesModel> geListOrderItemByCodes(GetListOrderItemByCodesParam getListOrderItemByCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl geListOrderItemByCodes param is {}", JSON.toJSONString(getListOrderItemByCodesParam));
        if (ObjectUtils.isEmpty(getListOrderItemByCodesParam)) {
            return ChannelListResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl geListOrderItemByCodes param is null");
        }
        if (ObjectUtils.isEmpty(getListOrderItemByCodesParam.getPage())) {
            getListOrderItemByCodesParam.setPage(0);
        }
        if (ObjectUtils.isEmpty(getListOrderItemByCodesParam.getPageSize()) || getListOrderItemByCodesParam.getPageSize() > 100) {
            getListOrderItemByCodesParam.setPageSize(100);
        }
        // 调用主线接口
        TypeReference<SunyurListResult<GetListOrderItemByCodesModel>> typeReference = new TypeReference<SunyurListResult<GetListOrderItemByCodesModel>>() {
        };
        SunyurListResult<GetListOrderItemByCodesModel> sunyurListResult = sunyurClient.execListPost(getOrderListOrderItemByCodes, JSON.toJSONString(getListOrderItemByCodesParam), typeReference);
        //判断是否出错
        if (!SunyurResultUtil.isSuccess(sunyurListResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurListResult.getErrorMessage());
        }
        // 判断是否存在下一页
        if (!SunyurResultUtil.isNextPage(sunyurListResult)) {
            return ChannelListResultUtil.success(sunyurListResult.getContent());
        }

        // 返回结果集
        List<GetListOrderItemByCodesModel> resultList = Lists.newArrayList();
        // 将第一次查询的结果收集
        resultList.addAll(sunyurListResult.getContent());
        // 循环查询剩余数据
        for (int i = getListOrderItemByCodesParam.getPage() + 1; i <= sunyurListResult.getTotalPage(); i++) {
            sunyurListResult.setPage(i);
            // 调用主线接口
            SunyurListResult<GetListOrderItemByCodesModel> sunyurPageResult = sunyurClient.execListPost(getOrderListOrderItemByCodes, JSON.toJSONString(getListOrderItemByCodesParam), typeReference);
            // 某次调用主线返回错误（小概率，可能网络等原因，此时结果集数据不完整，本次查询失败）
            if (!SunyurResultUtil.isSuccess(sunyurPageResult)) {
                LOGGER_ERROR.error("geListOrderItemByCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getListOrderItemByCodesParam), JSON.toJSONString(sunyurPageResult));
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurPageResult.getErrorMessage());
            }
            resultList.addAll(sunyurPageResult.getContent());
        }
        return ChannelListResultUtil.success(resultList);
    }

    /**
     * @Description 根据来源单号批量获取订单行信息接口
     * @Param getListOrderItemBySourceCodesParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.GetListOrderItemBySourceCodesModel>
     * @Author hao.tian
     * @Date 2021/5/11
     * @Version v1.0
     */
    @Override
    public ChannelResult<List<GetListOrderItemBySourceCodesModel>> getListOrderItemBySourceCodes(GetListOrderItemBySourceCodesParam getListOrderItemBySourceCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl getListOrderItemBySourceCodes param is {}", JSON.toJSONString(getListOrderItemBySourceCodesParam));
        if (ObjectUtils.isEmpty(getListOrderItemBySourceCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl getListOrderItemBySourceCodes param is null");
        }
        List<GetListOrderItemBySourceCodesModel> resultList = Lists.newArrayList();
        // 参数按长度分割
        List<List<String>> sourceCodes = Lists.partition(getListOrderItemBySourceCodesParam.getSourceCodes(), 100);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        GetListOrderItemBySourceCodesParam getListOrderItemBySourceCodesParamClient = new GetListOrderItemBySourceCodesParam();
        TypeReference<SunyurResult<List<GetListOrderItemBySourceCodesModel>>> typeReference = new TypeReference<SunyurResult<List<GetListOrderItemBySourceCodesModel>>>() {
        };
        for (List<String> sourceCode : sourceCodes) {
            getListOrderItemBySourceCodesParamClient.setSourceCodes(sourceCode);
            getListOrderItemBySourceCodesParamClient.setSourceType(getListOrderItemBySourceCodesParam.getSourceType());
            // 调用主线接口
            SunyurResult<List<GetListOrderItemBySourceCodesModel>> sunyurResult = sunyurClient.execPost(getListOrderItemBySourceCodes, JSON.toJSONString(getListOrderItemBySourceCodesParamClient), typeReference);
            if (SunyurResultUtil.isSuccess(sunyurResult)) {
                resultList.addAll(sunyurResult.getContent());
            } else {
                LOGGER_ERROR.error("getListOrderItemBySourceCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getListOrderItemBySourceCodesParamClient), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
           return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(resultList);
    }

    /**
     * @Description 根据来源行号批量获取订单行信息接口
     * @Param getListOrderItemBySourceItemCodesParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.GetListOrderItemBySourceItemCodesModel>
     * @Author hao.tian
     * @Date 2021/5/11
     * @Version v1.0
     */
    @Override
    public ChannelResult<List<GetListOrderItemBySourceItemCodesModel>> getlistOrderItemBySourceItemCodes(GetListOrderItemBySourceItemCodesParam getListOrderItemBySourceItemCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl getlistOrderItemBySourceItemCodes param is {}", JSON.toJSONString(getListOrderItemBySourceItemCodesParam));
        if (ObjectUtils.isEmpty(getListOrderItemBySourceItemCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl getlistOrderItemBySourceItemCodes param is null");
        }
        List<GetListOrderItemBySourceItemCodesModel> resultList = Lists.newArrayList();
        // 参数按长度分割
        List<List<String>> sourceCodes = Lists.partition(getListOrderItemBySourceItemCodesParam.getSourceItemCodes(), 100);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        GetListOrderItemBySourceItemCodesParam getListOrderItemBySourceItemCodesParamCilent = new GetListOrderItemBySourceItemCodesParam();
        TypeReference<SunyurResult<List<GetListOrderItemBySourceItemCodesModel>>> typeReference = new TypeReference<SunyurResult<List<GetListOrderItemBySourceItemCodesModel>>>() {
        };
        for (List<String> sourceCode : sourceCodes) {
            getListOrderItemBySourceItemCodesParamCilent.setSourceItemCodes(sourceCode);
            getListOrderItemBySourceItemCodesParamCilent.setSourceType(getListOrderItemBySourceItemCodesParam.getSourceType());
            // 调用主线接口
            SunyurResult<List<GetListOrderItemBySourceItemCodesModel>> sunyurResult = sunyurClient.execPost(getlistOrderItemBySourceItemCodes, JSON.toJSONString(getListOrderItemBySourceItemCodesParamCilent), typeReference);
            if (SunyurResultUtil.isSuccess(sunyurResult)) {
                resultList.addAll(sunyurResult.getContent());
            } else {
                LOGGER_ERROR.error("getlistOrderItemBySourceItemCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getListOrderItemBySourceItemCodesParamCilent), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
           return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(resultList);
    }

    /**
     * @Description 批量修改订单
     * @Param updateOrderListParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.searchorder.model.UpdateOrderListModel>
     * @Author hao.tian
     * @Date 2021/5/13
     * @Version v1.0
     */
    @Override
    public ChannelResult updateOrderList(List<UpdateOrderListParam> updateOrderListParamList) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl updateOrderList param is {}", JSON.toJSONString(updateOrderListParamList));
        if (ObjectUtils.isEmpty(updateOrderListParamList)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl updateOrderList param is null");
        }
        // 参数按长度分割
        List<List<UpdateOrderListParam>> updateOrderLists = Lists.partition(updateOrderListParamList, 100);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        for (List<UpdateOrderListParam> orderList : updateOrderLists) {
            // 调用主线接口
            SunyurResult sunyurResult = sunyurClient.execPost(updateOrderList, JSON.toJSONString(orderList), null);
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                LOGGER_ERROR.error("updateOrderList loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(orderList), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
           return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(null);
    }

    /**
     * @Description 批量修改订单结算
     * @Param updateOrdersParamList
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author hao.tian
     * @Date 2021/5/13
     * @Version v1.0
     */
    @Override
    public ChannelResult updateOrders(UpdateOrdersParam updateOrdersParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl updateOrders param is {}", JSON.toJSONString(updateOrdersParam));
        if (ObjectUtils.isEmpty(updateOrdersParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl updateOrders param is null");
        }
        // 参数按长度分割
        List<List<String>> orderCodesList = Lists.partition(updateOrdersParam.getOrderCodes(), 100);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        UpdateOrdersParam updateOrdersParamClient = new UpdateOrdersParam();
        for (List<String> strings : orderCodesList) {
            updateOrdersParamClient.setOrderCodes(strings);
            updateOrdersParamClient.setOperatorCode(updateOrdersParam.getOperatorCode());
            updateOrdersParamClient.setSettleCompanyCode(updateOrdersParam.getSettleCompanyCode());
            // 调用主线接口
            SunyurResult sunyurResult = sunyurClient.execPost(updateOrders, JSON.toJSONString(updateOrdersParamClient), null);
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                LOGGER_ERROR.error("updateOrders loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(updateOrdersParamClient), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
           return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(null);
    }

    /**
     * @Description 取消订单
     * @Param cancelOrderParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author hao.tian
     * @Date 2021/5/13
     * @Version v1.0
     */
    @Override
    public ChannelResult cancleOrder(CancelOrderParam cancelOrderParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl cancleOrder param is {}", JSON.toJSONString(cancelOrderParam));
        if (ObjectUtils.isEmpty(cancelOrderParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl cancleOrder param is null");
        }
        if (ObjectUtils.isEmpty(cancelOrderParam.getCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "订单取消接口订单号不能为空！");
        }
        if (ObjectUtils.isEmpty(cancelOrderParam.getOperatorCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "订单取消接口操作人不能为空！");
        }
        // 调用主线接口
        SunyurResult sunyurResult = sunyurClient.execPost(cancelOrder, JSON.toJSONString(cancelOrderParam), null);
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(null);
    }

    /**
     * @Description 订单整单删除
     * @Param deleteOrderByOrderCodesParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author hao.tian
     * @Date 2021/5/13
     * @Version v1.0
     */
    @Override
    public ChannelResult deleteOrderByOrderCodes(DeleteOrderByOrderCodesParam deleteOrderByOrderCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl deleteOrderByOrderCodes param is {}", JSON.toJSONString(deleteOrderByOrderCodesParam));
        if (ObjectUtils.isEmpty(deleteOrderByOrderCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl deleteOrderByOrderCodes param is null");
        }
        if (CollectionUtils.isEmpty(deleteOrderByOrderCodesParam.getOrderCodes())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "订单整单删除接口订单号不能为空！");
        }
        if (ObjectUtils.isEmpty(deleteOrderByOrderCodesParam.getOperatorCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "订单整单删除接口操作人不能为空！");
        }
        List<String> orderCodes = deleteOrderByOrderCodesParam.getOrderCodes();

        // 参数按长度分割
        List<List<String>> orderCodesList = Lists.partition(orderCodes, 20);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        DeleteOrderByOrderCodesParam deleteOrderByOrderCodesParamClient = new DeleteOrderByOrderCodesParam();
        for (List<String> strings : orderCodesList) {
            //将分割后订单号，操作人，备注拼成新的参数
            deleteOrderByOrderCodesParamClient.setOperatorCode(deleteOrderByOrderCodesParam.getOperatorCode());
            deleteOrderByOrderCodesParamClient.setOrderCodes(strings);
            deleteOrderByOrderCodesParamClient.setRemark(deleteOrderByOrderCodesParam.getRemark());
            // 调用主线接口
            SunyurResult sunyurResult = sunyurClient.execPost(deleteOrderByOrderCodes, JSON.toJSONString(deleteOrderByOrderCodesParamClient), null);
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                LOGGER_ERROR.error("deleteOrderByOrderCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(deleteOrderByOrderCodesParamClient), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
           return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(null);
    }

    /**
     * @Description 根据订单行编号删除订单行来源信息
     * @Param removeSourceByOrderItemCodesParam
     * @Return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author hao.tian
     * @Date 2021/5/13
     * @Version v1.0
     */
    @Override
    public ChannelResult removeSourceByOrderItemCodes(RemoveSourceByOrderItemCodesParam removeSourceByOrderItemCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl removeSourceByOrderItemCodes param is {}", JSON.toJSONString(removeSourceByOrderItemCodesParam));
        if (ObjectUtils.isEmpty(removeSourceByOrderItemCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl removeSourceByOrderItemCodes param is null");
        }
        if (CollectionUtils.isEmpty(removeSourceByOrderItemCodesParam.getOrderItemCodes())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "根据订单行编号删除订单行来源信息接口订单行号不能为空！");
        }
        if (ObjectUtils.isEmpty(removeSourceByOrderItemCodesParam.getOperatorCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "根据订单行编号删除订单行来源信息操作人不能为空！");
        }
        // 参数按长度分割
        List<List<String>> orderItemCodes = Lists.partition(removeSourceByOrderItemCodesParam.getOrderItemCodes(), 100);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        RemoveSourceByOrderItemCodesParam removeSourceByOrderItemCodesParamCilent = new RemoveSourceByOrderItemCodesParam();
        for (List<String> orderItemCode : orderItemCodes) {
            removeSourceByOrderItemCodesParamCilent.setOrderItemCodes(orderItemCode);
            removeSourceByOrderItemCodesParamCilent.setOperatorCode(removeSourceByOrderItemCodesParam.getOperatorCode());
            // 调用主线接口
            SunyurResult sunyurResult = sunyurClient.execPost(removeSourceByOrderItemCodes, JSON.toJSONString(removeSourceByOrderItemCodesParamCilent), null);
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                LOGGER_ERROR.error("removeSourceByOrderItemCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(removeSourceByOrderItemCodesParamCilent), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
           return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(null);
    }

    @Override
    public ChannelResult create(CreateOrderParam createOrderParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl create param is {}", JSON.toJSONString(createOrderParam));
        if (ObjectUtils.isEmpty(createOrderParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl create param is null");
        }

        // 调用主线接口
        SunyurResult sunyurResult = sunyurClient.execPost(createOrder, JSON.toJSONString(createOrderParam), null);
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }


    @Override
    public ChannelResult<List<ListOrderItemCountByCodesModel>> getListOrderItemCountByCodes(ListOrderItemCountByCodesParam listOrderItemCountByCodesParam) throws Exception {
        if (ObjectUtils.isEmpty(listOrderItemCountByCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl getListOrderItemCountByCodes param is null");
        }
        List<ListOrderItemCountByCodesModel> resultList = Lists.newArrayList();
        // 参数按长度分割
        List<List<String>> orderCodes = Lists.partition(listOrderItemCountByCodesParam.getOrderItemCodes(), 45);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        ListOrderItemCountByCodesParam getListOrderByCodesParamClilent = new ListOrderItemCountByCodesParam();
        TypeReference<SunyurResult<List<ListOrderItemCountByCodesModel>>> typeReference = new TypeReference<SunyurResult<List<ListOrderItemCountByCodesModel>>>() {
        };
        for (List<String> orderCode : orderCodes) {
            getListOrderByCodesParamClilent.setOrderItemCodes(orderCode);
            // 调用主线接口
            SunyurResult<List<ListOrderItemCountByCodesModel>> sunyurResult = sunyurClient.execPost(getListOrderItemCountByCodes, JSON.toJSONString(getListOrderByCodesParamClilent), typeReference);
            if (SunyurResultUtil.isSuccess(sunyurResult)) {
                resultList.addAll(sunyurResult.getContent());
            } else {
                LOGGER_ERROR.error("getListOrderItemCountByCodes loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getListOrderByCodesParamClilent), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(resultList);
    }


    @Override
    public ChannelResult updateOrderItems(List<UpdateOrderItemsParam> updateOrderItemsParamList) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl updateOrderItemsParamList param is {}", JSON.toJSONString(updateOrderItemsParamList));
        if (CollectionUtils.isEmpty(updateOrderItemsParamList)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl updateOrderItemsParamList param is null");
        }
        // 参数按长度分割
        List<List<UpdateOrderItemsParam>> partition = Lists.partition(updateOrderItemsParamList, 20);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        for (int i = 0; i < partition.size(); i++) {
            List<UpdateOrderItemsParam> updateOrderItemsParams = partition.get(i);
            // 调用主线接口
            SunyurResult sunyurResult = sunyurClient.execPost(updateOrderItems, JSON.toJSONString(updateOrderItemsParams), null);
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                LOGGER_ERROR.error("updateOrderItems loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(updateOrderItemsParams), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(true);
    }

    @Override
    public ChannelResult getOrderItemCount(GetListOrderItemByCodesParam getListOrderItemByCodesParam) throws Exception {
        LOGGER_INFO.info("SunyurOrderServiceImpl getOrderItemCount param is {}", JSON.toJSONString(getListOrderItemByCodesParam));
        if (ObjectUtils.isEmpty(getListOrderItemByCodesParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), "SunyurOrderServiceImpl getListOrderItemByCodesParam param is null");
        }
        if (ObjectUtils.isEmpty(getListOrderItemByCodesParam.getPage())) {
            getListOrderItemByCodesParam.setPage(0);
        }
        if (ObjectUtils.isEmpty(getListOrderItemByCodesParam.getPageSize()) || getListOrderItemByCodesParam.getPageSize() > 100) {
            getListOrderItemByCodesParam.setPageSize(100);
        }
        // 调用主线接口
        TypeReference<SunyurListResult<OrderItemCountModel>> typeReference = new TypeReference<SunyurListResult<OrderItemCountModel>>() {
        };
        SunyurListResult<OrderItemCountModel> sunyurListResult = sunyurClient.execListPost(getOrderListOrderItemByCodes, JSON.toJSONString(getListOrderItemByCodesParam), typeReference);
        //判断是否出错
        if (!SunyurResultUtil.isSuccess(sunyurListResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurListResult.getErrorMessage());
        }
        // 判断是否存在下一页
        if (!SunyurResultUtil.isNextPage(sunyurListResult)) {
            return ChannelListResultUtil.success(sunyurListResult.getContent());
        }

        // 返回结果集
        List<OrderItemCountModel> resultList = Lists.newArrayList();
        // 将第一次查询的结果收集
        resultList.addAll(sunyurListResult.getContent());
        // 循环查询剩余数据
        for (int i = getListOrderItemByCodesParam.getPage() + 1; i <= sunyurListResult.getTotalPage(); i++) {
            sunyurListResult.setPage(i);
            // 调用主线接口
            SunyurListResult<OrderItemCountModel> sunyurPageResult = sunyurClient.execListPost(getOrderItemCount, JSON.toJSONString(getListOrderItemByCodesParam), typeReference);
            // 某次调用主线返回错误（小概率，可能网络等原因，此时结果集数据不完整，本次查询失败）
            if (!SunyurResultUtil.isSuccess(sunyurPageResult)) {
                LOGGER_ERROR.error("getOrderItemCount loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(getListOrderItemByCodesParam), JSON.toJSONString(sunyurPageResult));
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurPageResult.getErrorMessage());
            }
            resultList.addAll(sunyurPageResult.getContent());
        }
        return ChannelListResultUtil.success(resultList);
    }
}
